//
//  LHStatusesCell.swift
//  sinaWeibo
//
//  Created by LiHong on 15/11/1.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

class LHStatusesCell: UITableViewCell {

    //MARK: - 属性
    var pictureViewWidthCon: NSLayoutConstraint?
    
    var pictureViewHeightCon: NSLayoutConstraint?
    
    var statuses: LHStatus? {
        didSet{
            topView.statuses = statuses
            ///微博内容
            contentLabel.text = statuses?.text
            
         //   pictureViewHeightCon?.constant = CGFloat(arc4random_uniform(3)) * 90
            pictureView.status = statuses
            let size = pictureView.calcViewSize()
            
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
            
        }
    }
    
    func rowHeight(status: LHStatus) -> CGFloat {
        self.statuses = status
        
        layoutIfNeeded()
        
        let maxY = CGRectGetMaxY(bottomView.frame)
        return maxY
    }

    
    //MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///布局UI
    func prepareUI(){
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        //MARK: - 添加约束
        // topView添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        // 内容添加约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        // 内容宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
//        // 配图视图添加约束
//        let con = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: 8))
//        
//        pictureViewWidthCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Height)
        
        // 底部视图添加约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44.0), offset: CGPoint(x: -8, y: 8))
        
        // 设置内容大小
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
        
        // contenView底部与内容重合
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
    }

    //MARK: - 懒加载
    /// 微博配图视图
    lazy var pictureView: LHPictureView = LHPictureView()
    
    /// 底部视图
    lazy var bottomView: UIView = LHBottomView()
    
    /// 头部视图
    private lazy var topView: LHTopView = LHTopView()
    
    ///内容
    lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 16, textColor: UIColor.blackColor())
        label.numberOfLines = 0
        return label
    }()
    

    
}
